package com.demo.shareding.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.demo.shareding.domain.Course;
import com.demo.shareding.mapper.CourseMapper;
import io.swagger.annotations.ApiOperation;
import org.apache.shardingsphere.api.hint.HintManager;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: SharedingController
 * @Description:
 * @Author: 橙哈哈
 * @Date: 2025-04-07 21:52
 **/
@RestController
@RequestMapping("demo")
public class SharedingController {


    @Resource
    private CourseMapper courseMapper;

    //======================测试水平分库=====================
    //添加操作
    @GetMapping("addCourseDb")
    @ApiOperation(value = "水平分库添加课程")
    public void addCourseDb() {
        for(int i=1;i<=55;i++) {
            Course course = new Course();
            course.setCname("java"+i);
            course.setUserId(Long.valueOf(i));
            course.setCstatus("Normal"+i);
            courseMapper.insert(course);
        }

    }
    //查询操作
    @GetMapping("findCourseDb")
    @ApiOperation(value = "水平分库查询")
    public void findCourseDb() {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        //设置 userid 值
        wrapper.eq("user_id", 100L);
        //设置 cid 值
        wrapper.eq("cid", 465162909769531393L);
        Course course = courseMapper.selectOne(wrapper);
        System.out.println(course);
    }



    //=======================测试水平分表===================
    //添加课程的方法 split-hor
    @GetMapping("addCourse")
    @ApiOperation(value = "添加课程")
    public void addCourse() {
        for(int i=1;i<=10;i++) {
            Course course = new Course();
            course.setCname("java"+i);
            course.setUserId(Long.valueOf(i));
            course.setCstatus("Normal"+i);
            courseMapper.insert(course);
        }
    }
    //查询课程的方法
    @GetMapping("findCourse")
    @ApiOperation(value = "查询课程的方法")
    public void findCourse() {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("cid",598159790354464769L);
        Course course = courseMapper.selectOne(wrapper);
        System.out.println(course);
    }
    /*
    inline 不支持范围查询
     */
    @GetMapping("queryOrderRange")
    @ApiOperation(value = "inline不支持范围查询")
    public void queryOrderRange(){
        //select * from course
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.between("cid",553684818806706177L,553684819184193537L);
//        wrapper.in()
        List<Course> courses = courseMapper.selectList(wrapper);
        courses.forEach(course -> System.out.println(course));
    }

    @GetMapping("queryCourseComplex")
    @ApiOperation(value = "")
    public void queryCourseComplex(){
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.between("cid",553684818806706177L,553684819184193537L);
        wrapper.eq("user_id",1009L);
//        wrapper.in()
        List<Course> courses = courseMapper.selectList(wrapper);
        courses.forEach(course -> System.out.println(course));
    }
    /*
    强制路由
     */
    @GetMapping("queryCourseByHint")
    @ApiOperation(value = "强制路由")
    public void queryCourseByHint(){
        HintManager hintManager = HintManager.getInstance();
        hintManager.addTableShardingValue("course",2);// 表名 值
        List<Course> courses = courseMapper.selectList(null);
        courses.forEach(course -> System.out.println(course));
        hintManager.close();
    }


}
